package com.sleepsocial.web.authentication;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.sleepsocial.authentication.AuthenticationException;
import com.sleepsocial.authentication.AuthenticationManager;
import com.sleepsocial.authentication.SignupException;
import com.sleepsocial.logging.Logger;
import com.sleepsocial.persistency.entity.user.UserProfile;
import com.sleepsocial.web.Web;
import com.sleepsocial.web.error.WebError;

@Controller
public class WebAuthenticationController extends Web {

    public static final Logger  logger         = Logger.getInstance(WebAuthenticationController.class.getSimpleName());

    public static final String  LOGIN_URL      = "/login";
    public static final String  LOGOUT_URL     = "/logout";
    public static final String  SIGNUP_URL     = "/signup";

    private static final String LOGIN_VIEW     = "login";
    private static final String LOGIN_REDIRECT = "redirect:web/dashboard";

    @RequestMapping(value = LOGIN_URL, method = RequestMethod.POST)
    public String doLogin(@RequestParam String email, @RequestParam String password, HttpServletRequest req, HttpServletResponse res, Model model) {
        UserProfile profile = null;

        if (AuthenticationManager.isUserAuthenticated(req)) {
            return LOGIN_REDIRECT;
        } else {

            try {
                profile = AuthenticationManager.authenticate(email, password, res);
            } catch (AuthenticationException e) {
                model.addAttribute(WebError.ERROR_ATTRIBUTE, new WebError(e));
                logger.i("unable to login", e);
            }

            if (profile != null) {
                UserProfile user = AuthenticationManager.getAuthenticatedUser(req);
                model.addAttribute("user", user);

                return LOGIN_REDIRECT;
            } else {
                return LOGIN_VIEW;
            }
        }

    }

    @RequestMapping(value = LOGIN_URL, method = RequestMethod.GET)
    public String showLoginForm(Model model) {
        return LOGIN_VIEW;
    }

    @RequestMapping(value = SIGNUP_URL, method = RequestMethod.POST)
    public String signup(@RequestParam String email, @RequestParam String password, HttpServletResponse res, Model model) {
        try {
            AuthenticationManager.signup(email, password);
            AuthenticationManager.authenticate(email, password, res);
        } catch (SignupException e) {
            model.addAttribute(WebError.ERROR_ATTRIBUTE, new WebError(e));
            logger.i("unable to signup", e);
            return LOGIN_VIEW;
        } catch (AuthenticationException e) {
            model.addAttribute(WebError.ERROR_ATTRIBUTE, new WebError(e));
            logger.i("unable to authenticate", e);
            return LOGIN_VIEW;
        }

        return LOGIN_REDIRECT;

    }

    @RequestMapping(value = LOGOUT_URL)
    public String logout(HttpServletResponse res) {
        AuthenticationManager.logout(res);
        return LOGIN_VIEW;
    }

}
